<script setup>
import { ref, onMounted, watch } from 'vue'

// 主题配置
const themeConfig = ref({
  mode: 'light', // light or dark
  primaryColor: '#1890ff',
  menuTheme: 'dark', // dark or light
  colorWeakness: false, // 色弱模式
  compactMode: false, // 紧凑模式
  borderRadius: 4, // 圆角大小
})

// 预设主题色
const presetColors = [
  '#1890ff', // 默认蓝色
  '#f5222d', // 红色
  '#fa8c16', // 橙色
  '#faad14', // 黄色
  '#52c41a', // 绿色
  '#13c2c2', // 青色
  '#2f54eb', // 蓝色
  '#722ed1', // 紫色
]

// 应用主题设置
const applyTheme = () => {
  // console.log('应用主题设置:', themeConfig.value)
  
  // 设置主题模式
  document.documentElement.setAttribute('data-theme', themeConfig.value.mode)
  
  // 设置主题色
  document.documentElement.style.setProperty('--primary-color', themeConfig.value.primaryColor)
  
  // 设置圆角
  document.documentElement.style.setProperty('--border-radius', `${themeConfig.value.borderRadius}px`)
  
  // 设置色弱模式
  if (themeConfig.value.colorWeakness) {
    document.documentElement.classList.add('color-weakness')
  } else {
    document.documentElement.classList.remove('color-weakness')
  }
  
  // 设置紧凑模式
  if (themeConfig.value.compactMode) {
    document.documentElement.classList.add('compact-mode')
  } else {
    document.documentElement.classList.remove('compact-mode')
  }
  
  // 保存设置到本地存储
  localStorage.setItem('themeConfig', JSON.stringify(themeConfig.value))
}

// 加载保存的主题设置
onMounted(() => {
  const savedTheme = localStorage.getItem('themeConfig')
  if (savedTheme) {
    themeConfig.value = JSON.parse(savedTheme)
    applyTheme()
  }
})

// 监听主题变化
watch(themeConfig, () => {
  applyTheme()
}, { deep: true, immediate: true })
</script>

<template>
  <div class="system-page">
    <div class="setting-card">
      <h2>主题设置</h2>
      
      <div class="setting-section">
        <h3>主题模式</h3>
        <div class="theme-mode-selector">
          <div
            class="mode-item"
            :class="{ active: themeConfig.mode === 'light' }"
            @click="themeConfig.mode = 'light'"
          >
            <i class="fa-solid fa-sun"></i>
            <span>浅色模式</span>
          </div>
          <div
            class="mode-item"
            :class="{ active: themeConfig.mode === 'dark' }"
            @click="themeConfig.mode = 'dark'"
          >
            <i class="fa-solid fa-moon"></i>
            <span>深色模式</span>
          </div>
        </div>
      </div>
      
      <div class="setting-section">
        <h3>主题色</h3>
        <div class="color-picker">
          <div
            v-for="color in presetColors"
            :key="color"
            class="color-item"
            :style="{ backgroundColor: color }"
            :class="{ active: themeConfig.primaryColor === color }"
            @click="themeConfig.primaryColor = color"
          ></div>
        </div>
      </div>
      
      <div class="setting-section">
        <h3>界面设置</h3>
        <div class="setting-item">
          <span>菜单主题色</span>
          <select v-model="themeConfig.menuTheme">
            <option value="dark">深色</option>
            <option value="light">浅色</option>
          </select>
        </div>
        
        <div class="setting-item">
          <span>色弱模式</span>
          <label class="switch">
            <input 
              type="checkbox" 
              v-model="themeConfig.colorWeakness"
            >
            <span class="slider"></span>
          </label>
        </div>
        
        <div class="setting-item">
          <span>紧凑模式</span>
          <label class="switch">
            <input 
              type="checkbox" 
              v-model="themeConfig.compactMode"
            >
            <span class="slider"></span>
          </label>
        </div>
        
        <div class="setting-item">
          <span>圆角大小</span>
          <div class="radius-slider">
            <input 
              type="range" 
              v-model="themeConfig.borderRadius" 
              min="0" 
              max="16"
            >
            <span>{{ themeConfig.borderRadius }}px</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.system-page {
  padding: 24px;
}

.setting-card {
  background: var(--bg-color);
  border-radius: var(--border-radius);
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  border: 1px solid var(--border-color);
}

h2 {
  margin: 0 0 24px 0;
  font-size: 20px;
  font-weight: 500;
  color: var(--text-color);
}

.setting-section {
  margin-bottom: 32px;
}

h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 500;
  color: var(--text-color);
}

.theme-mode-selector {
  display: flex;
  gap: 16px;
}

.mode-item {
  flex: 1;
  padding: 16px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s;
  color: var(--text-color);
  background: var(--bg-color);
}

.mode-item:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.mode-item.active {
  background: var(--primary-color);
  border-color: var(--primary-color);
  color: white;
}

.color-picker {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.color-item {
  width: 24px;
  height: 24px;
  border-radius: var(--border-radius);
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
}

.color-item::after {
  content: '';
  position: absolute;
  inset: -2px;
  border: 2px solid transparent;
  border-radius: calc(var(--border-radius) + 2px);
  transition: all 0.3s;
}

.color-item:hover::after {
  border-color: var(--primary-color);
}

.color-item.active::after {
  border-color: var(--primary-color);
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  color: var(--text-color);
}

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 20px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: var(--border-color);
  transition: .4s;
  border-radius: 20px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--primary-color);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

.radius-slider {
  display: flex;
  align-items: center;
  gap: 12px;
}

.radius-slider input {
  width: 200px;
  background: var(--bg-color);
  border: 1px solid var(--border-color);
}

select {
  padding: 4px 8px;
  border: 1px solid var(--border-color);
  border-radius: var(--border-radius);
  background: var(--bg-color);
  color: var(--text-color);
}

/* 深色模式下的特殊样式 */
:global([data-theme='dark']) .setting-card {
  background: var(--bg-color);
}

:global([data-theme='dark']) .mode-item {
  background: var(--bg-color);
}

:global([data-theme='dark']) select option {
  background: var(--bg-color);
}

:global([data-theme='dark']) .slider:before {
  background-color: #f0f0f0;
}
</style> 